; (function (undefined) {
  "use strict"
  let _global;

  function isWeixin() {
    const ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
      return true;
    } else {
      return false;
    }
  }

  /**获取URL参数 */
  function getUrlParams(param) {
    let reg = new RegExp("(^|&)" + param + "=([^&]*)(&|$)");
    let local = window.location;
    let r = local.search.substr(1).match(reg);
    if (r != null)
      return decodeURI(r[2]);//对encodeURI编码处理过的uri进行解码
    return null;
  }

  //error note:
  //err1: {"errMsg":"onMenuShareTimeline:fail, the permission value is offline verifying"}
  //err2: {"errMsg":"updateTimelineShareData:permission denied"}
  //err3: invalid url domain
  //err4: 分享无反应-因为用了最新的基础方法【updateXxxxxXx()...】
 
  /**
   * 微信分享分装-传入必要参数
   * @param {*} shareParams 
   */
  function Share(shareParams = {}) {
    // alert("shareParams2:"+JSON.stringify(shareParams))
    const { title, desc, link, imgUrl } = shareParams;
    let currUrl = encodeURIComponent(location.href.split('#')[0]); //后端签名用的URL需要动态获取，并在传递的时候进行编/解码
    axios.get('/wxconfig?url='+currUrl).then(res => {
      if (res.data.error == 1) {
        alert(res.data.msg)
        return;
      }

      // alert('wxconfig:' + JSON.stringify(res.data));
      let data = res.data.data;
      wx.config({
        debug: false,
        appId: data.appId,
        timestamp: data.timestamp,
        nonceStr: data.nonceStr, 
        signature: data.signature,
        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'showMenuItems'] // 必填
      });

      wx.ready(function () {
        // wx.checkJsApi({
        //   jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'showMenuItems'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
        //   success: function(res) { 
        //       alert('checkJsApi res:'+JSON.stringify(res))
        //     }
        // });

        // 隐藏所有非基础按钮接口
        wx.hideAllNonBaseMenuItem();

        //显示分享选项
        wx.showMenuItems({
          menuList: ['menuItem:share:appMessage', 'menuItem:share:timeline'] // 要显示的菜单项，所有menu项见附录3
        });

        //分享给朋友
        wx.onMenuShareAppMessage({
          title: title,
          desc: desc,
          link: link,
          imgUrl: imgUrl,
          type: 'link', // 分享类型,music、video或link，不填默认为link
          dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
          success: function () { 
            
          },
          cancel: function () {
            // 用户取消分享后执行的回调函数 
          }
        });

        //分享到朋友圈
        wx.onMenuShareTimeline({
          title: title,
          link: link,
          imgUrl: imgUrl,
          success: function () {
            
          },
          cancel: function () {
            // 用户取消分享后执行的回调函数
          }
        });
      });

      wx.error(function (err) {
        alert('wx config err:'+JSON.stringify(err))
        // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
      });
    }).catch(error => {
      alert('get wxconfig error happend:'+ JSON.stringify(error));
    });
  }

  function WxShare() { }

  WxShare.prototype.isWeixin = isWeixin
  WxShare.prototype.Share = Share


  // 最后将插件对象暴露给全局对象
  _global = (function () { return this || (0, eval)('this'); }());
  if (typeof module !== "undefined" && module.exports) {
    module.exports = WxShare;
  }
  if (typeof define === "function" && define.amd) {
    define(function () { return WxShare; });
  }
  !('WxShare' in _global) && (_global.WxShare = WxShare);
}()); 